<template>
  <div>
    <el-card style="margin: 15px 0">
      <!-- 头部 -->
      <div slot="header" class="clearfix">
        <!-- @tab-click="handleClick" -->
        <el-tabs v-model="activeName" class="tabs">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visite"></el-tab-pane>
        </el-tabs>
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <!-- v-model="value1" -->
          <el-date-picker type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" class="date" size="mini" v-model="date" value-format="yyyy-MM-dd"> </el-date-picker>
        </div>
      </div>
      <el-row :gutter="10">
        <el-col :span="18">
          <div class="myecharts" ref="myecharts"></div>
        </el-col>
        <el-col :span="6">
          <div class="rank">
            <h4>门店{{ title }}排名</h4>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span>肯德基</span>
                <span class="rvalue">2341</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span>金拱门</span>
                <span class="rvalue">1234</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span>披萨</span>
                <span class="rvalue">4236</span>
              </li>
              <li>
                <span>4</span>
                <span>地锅鸡</span>
                <span class="rvalue">6789</span>
              </li>
              <li>
                <span>5</span>
                <span>炸鸡</span>
                <span class="rvalue">4356</span>
              </li>
              <li>
                <span>6</span>
                <span>面条</span>
                <span class="rvalue">3672</span>
              </li>
              <li>
                <span>7</span>
                <span>快餐</span>
                <span class="rvalue">1345</span>
              </li>
            </ul>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myecharts: null,
      date: [],
    }
  },
  computed: {
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    ...mapState({ list: (state) => state.home.list }),
  },
  watch: {
    title() {
      this.myecharts.setOption({
        title: {
          text: `${this.title}趋势`,
        },
        xAxis: [
          {
            type: 'category',
            data: this.title == '销售额' ? this.list.orderFullYearAxis : this.list.userFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.title == '销售额' ? this.list.orderFullYear : this.list.userFullYear,
          },
        ],
      })
    },
  },
  mounted() {
    this.myecharts = echarts.init(this.$refs.myecharts)
    this.myecharts.setOption({
      title: {
        text: `${this.title}趋势`,
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow',
        },
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: [
        {
          type: 'category',
          data: this.list.orderFullYearAxis,
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
        },
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: this.list.orderFullYear,
        },
      ],
    })
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY')
      this.date = [start, end]
    },
  },
}
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tabs {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.date {
  width: 220px;
}
.right span {
  margin: 0 10px;
  font-size: 14px;
}
.myecharts {
  width: 100%;
  height: 400px;
}
.rank h4 {
  margin-top: 0px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
.rank ul li {
  height: 17%;
  width: 100%;
}
.rank ul li span {
  padding: 10px 5px;
}
.rindex {
  float: left;
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  color: #fff;
  text-align: center;
  line-height: 0px;
}
.rvalue {
  float: right;
  line-height: 0;
}
</style>